package com.lotterymanage.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lotterymanage.dao.TreeDao;
import com.lotterymanage.domain.InsideUser;
import com.lotterymanage.domain.Tree;

/**
 * 目录管理.
 * @author lxz
 * @date Apr 21, 2010
 */
@Controller
@RequestMapping("/tree.do")
public class TreeController extends BaseController {

    private static final Logger LOG = Logger.getLogger(InsideUserController.class);
    @Autowired
    private TreeDao treeDao;

    /**
     * 后台主目录.
     * @param model ModelMap
     * @param request HttpServletRequest
     * @return left.html
     * @throws Exception Exception
     */
    @RequestMapping(params = "method=tree")
    public String tree(final ModelMap model, final HttpServletRequest request) throws Exception {
        InsideUser insideUser = (InsideUser) sessionGetAttr(request, "insideUserSession");
        List<Tree> treeList = treeDao.getInsideUserTree(insideUser);
        StringBuffer tree = new StringBuffer();
        if (treeList != null) {
            for (int i = 0; i < treeList.size(); i++) {
                Tree t = (Tree) treeList.get(i);
                tree.append("\n d.add(");
                tree.append(t.getTreeID());
                tree.append(",");
                tree.append(t.getTreeFID());
                tree.append(",'");
                tree.append(t.getTreeName());
                tree.append("','");
                if (t.getTreeSRC() != null) {
                    tree.append(contextPath(request) + "/");
                    tree.append(t.getTreeSRC());
                }
                tree.append("');");
            }
        }
        LOG.info("tree: " + tree);
        model.put("tree", tree);
        return "left";
    }

}
